import request from '@/utils/request';

// 短视频相关接口类型定义
export interface ShortVideo {
  video_id?: number;
  title?: string;
  description?: string;
  cover_image_id?: number;
  cover_image?: {
    upload_id: number;
    fileurl: string;
  };
  video_upload_id?: number;
  user_id?: number;
  like_count?: number;
  comment_count?: number;
  play_count?: number;
  is_public?: boolean;
  created_at?: string;
  updated_at?: string;
  createdAt?: string;
  updatedAt?: string;
  author?: {
    user_id: number;
    name: string;
    avatar?: string;
  };
  video_file?: {
    upload_id: number;
    fileurl: string;
    mimetype?: string;
  };
  is_liked?: boolean;
}

export interface ShortVideoComment {
  comment_id?: number;
  content: string;
  user_id?: number;
  video_id?: number;
  reply_to_comment_id?: number;
  created_at?: string;
  updated_at?: string;
  createdAt?: string;
  updatedAt?: string;
  author?: {
    user_id: number;
    name: string;
    avatar?: string;
  };
  replyTo?: {
    comment_id?: number;
    author?: {
      user_id: number;
      name: string;
      avatar?: string;
    };
  };
  replies?: ShortVideoComment[];
}

export interface ShortVideoLike {
  like_id?: number;
  user_id?: number;
  video_id?: number;
  created_at?: string;
  updated_at?: string;
  createdAt?: string;
  updatedAt?: string;
  user?: {
    user_id: number;
    name: string;
    avatar?: string;
  };
}

// 短视频相关接口
export const createShortVideo = (data: { 
  title?: string; 
  description?: string; 
  cover_image_id?: number; 
  video_upload_id: number 
}) => {
  return request.post<ShortVideo>('/short_video/create', data);
};

export const getShortVideoById = (data: { video_id: number }) => {
  return request.post<ShortVideo>('/short_video/detail', data);
};

export const getShortVideoList = (data: { page?: number; limit?: number; keyword?: string; sort_field?: string; sort_order?: string, source?: string }) => {
    // 设置默认值
  const params = { source: 'management_system', ...data };
  return request.post<{ list: ShortVideo[]; pagination: any }>('/short_video/list', params);
};

export const getShortVideoListAll = (data: { keyword?: string; sort_field?: string; sort_order?: string, source?: string }) => {
    // 设置默认值
  const params = { source: 'management_system', ...data };
  return request.post<ShortVideo[]>('/short_video/listAll', params);
};

export const deleteShortVideo = (data: { video_id: number }) => {
  return request.post('/short_video/delete', data);
};

export const batchDeleteShortVideo = (data: { video_ids: number[] }) => {
  return request.post('/short_video/batchDelete', data);
};

export const editShortVideo = (data: { 
  video_id: number;
  title?: string; 
  description?: string; 
  cover_image_id?: number;
}) => {
  return request.post<ShortVideo>('/short_video/edit', data);
};

export const playShortVideo = (data: { video_id: number }) => {
  return request.post<{ play_count: number }>('/short_video/play', data);
};

// 评论相关接口
export const createShortVideoComment = (data: { 
  video_id: number; 
  content: string; 
  reply_to_comment_id?: number 
}) => {
  return request.post<ShortVideoComment>('/short_video/comment/create', data);
};

export const getShortVideoCommentList = (data: { 
  video_id: number; 
  page?: number; 
  limit?: number 
}) => {
  return request.post<{ 
    list: ShortVideoComment[]; 
    pagination: any 
  }>('/short_video/comment/list', data);
};

export const deleteShortVideoComment = (data: { comment_id: number }) => {
  return request.post('/short_video/comment/delete', data);
};

// 点赞相关接口
export const likeShortVideo = (data: { video_id: number }) => {
  return request.post<ShortVideoLike>('/short_video/like/create', data);
};

export const unlikeShortVideo = (data: { video_id: number }) => {
  return request.post('/short_video/like/delete', data);
};

export const getShortVideoLikeList = (data: { 
  video_id: number; 
  page?: number; 
  limit?: number 
}) => {
  return request.post<{ 
    list: ShortVideoLike[]; 
    pagination: any 
  }>('/short_video/like/list', data);
};